<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label="作者" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入作者ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
        <el-select v-model="queryParams.userId" placeholder="请选择作者">
          <el-option
            v-for="item in vlogList2"
            :key="item.userId"
            :label="item.user.userName"
            :value="item.userId"
          >
          </el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布地点" prop="addr">
        <el-input
          v-model="queryParams.addr"
          placeholder="请输入发布地点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关联栏目" prop="relatedColumn">
        <!-- <el-input
          v-model="queryParams.relatedColumn"
          placeholder="请输入关联栏目 1美食  2旅游"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select
          v-model="queryParams.relatedColumn"
          placeholder="请选择关联栏目"
        >
          <el-option
            v-for="item in [
              { value: '1', label: '美食' },
              { value: '2', label: '旅游' },
              { value: '3', label: '社群' },
            ]"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="三级门店ID" prop="subId">
        <el-input
          v-model="queryParams.subId"
          placeholder="请输入三级门店ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:vlog:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:vlog:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:vlog:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:vlog:export']"
          >导出</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleDaoru"
          v-hasPermi="['system:vlog:export']"
          >导入</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="vlogList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="作品编号" align="center" prop="id" />
      <el-table-column label="作者" align="center">
        <!-- <template slot-scope="scope">{{ scope.row.user.userName }}</template> -->
      </el-table-column>
      <el-table-column label="标题" align="center" prop="title" />
      <!-- <el-table-column label="场景描述" align="center" prop="sceneDesc">
        <template slot-scope="scope">
          <el-popover
            placement="bottom"
            title="场景描述"
            width="200"
            trigger="click"
            :content="scope.row.sceneDesc"
          >
            <p class="desc_p" slot="reference">{{ scope.row.sceneDesc }}</p>
          </el-popover>
        </template>
      </el-table-column> -->
      <el-table-column
        label="封面图"
        align="center"
        prop="coverIcon"
        width="100"
      >
        <template slot-scope="scope">
          <image-preview
            :src="api_host + scope.row.coverIcon"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>
      <el-table-column label="发布地点" align="center" prop="addr" />
      <el-table-column label="关联栏目" align="center" prop="relatedColumn">
        <template slot-scope="scope">
          <el-tag type="warning" size="mini" v-if="scope.row.relatedColumn == 1"
            >美食</el-tag
          >
          <el-tag type="" size="mini" v-if="scope.row.relatedColumn == 2"
            >旅游</el-tag
          >
          <el-tag type="" size="mini" v-if="scope.row.relatedColumn == 3"
            >社群</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column label="作品标签" align="center" prop="vlogLabel" />
      <el-table-column label="浏览量" align="center" prop="viewVolume" />
      <el-table-column label="点赞量" align="center" prop="likeVolume" />
      <el-table-column label="收藏量" align="center" prop="collectionVolume" />
      <el-table-column label="评论量" align="center" prop="observeNumber" />
      <el-table-column label="作品状态" align="center" prop="vlogStatus">
        <template slot-scope="scope">
          <el-tag type="success" size="mini" v-if="scope.row.vlogStatus == 1"
            >已发布</el-tag
          >
          <el-tag type="info" size="mini" v-if="scope.row.vlogStatus == 2"
            >草稿</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="isAudit">
        <template slot-scope="scope">
          <el-tag type="danger" size="mini" v-if="scope.row.isAudit == 0"
            >未审核</el-tag
          >
          <el-switch
            v-else
            v-model="scope.row.isAudit"
            active-color="#13ce66"
            :active-value="1"
            :inactive-value="2"
            inactive-color="#999"
            @change="changeisDis(scope.row)"
          >
          </el-switch>
          <!-- <el-tag
            v-if="scope.row.isAudit == 1"
            type="success"
            size="mini"
            @click="changeIsAudit(scope.row)"
            >通过</el-tag
          >
          <el-tag type="warning" size="mini" v-if="scope.row.isAudit == 2"
            >未通过</el-tag
          > -->
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            :icon="scope.row.isAudit == 0 ? 'el-icon-edit' : 'el-icon-view'"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:comment:edit']"
            >{{ scope.row.isAudit == 0 ? "审核" : "查看" }}</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:vlog:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- <el-dialog title="温馨提示" :visible.sync="dialogVisible" width="30%">
      <span>您确定要将审核状态修改为不通过吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleChangeAduit">确 定</el-button>
      </span>
    </el-dialog> -->

    <el-dialog title="导入作品数据" :visible.sync="open2">
      <file-upload></file-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改用户发布信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="作者ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入作者ID" disabled />
        </el-form-item> -->
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="场景描述" prop="sceneDesc">
          <!-- <el-input
            v-model="form.sceneDesc"
            type="textarea"
            placeholder="请输入内容"
          /> -->
          <editor v-model="form.sceneDesc" :min-height="192" />
        </el-form-item>
        <el-form-item label="发布者">
          <!-- <el-select
            v-model="form.userId"
            placeholder="请选择该作品发布者"
            style="width: 100%"
            filterable
          >
            <el-option
              v-for="(item, index) in userList"
              :key="item.id"
              :label="item.userName"
              :value="item.id"
            />
          </el-select> -->
        </el-form-item>
        <el-form-item label="封面图">
          <image-upload v-model="form.coverIcon" :limit="1" />
        </el-form-item>
        <el-form-item
          label="资源内容"
          prop="resContent"
          v-if="showuploads == 0"
        >
          <image-upload v-model="form.resContent" />
        </el-form-item>
        <el-form-item
          label="资源内容"
          prop="resContent"
          v-if="form.resType == 1 && showuploads == 1"
        >
          <image-upload v-model="form.resContent" />
          <!-- <template v-if="form.resContent">
            <image-preview
              v-for="(item, index) in form.resContent.split(',')"
              :src="api_host + item"
              :width="100"
              :height="100"
            />
          </template> -->
        </el-form-item>
        <el-form-item
          label="资源内容"
          prop="resContent"
          v-if="form.resType != 1 && showuploads == 1"
        >
          <!-- <image-upload v-model="form.resContent"/> -->
          <video
            :src="api_host + form.resContent"
            controls="controls"
            style="width: 100%"
          ></video>
        </el-form-item>
        <el-form-item label="点赞量">
          <el-input v-model="form.likeVolume" placeholder="请输入点赞量" />
        </el-form-item>
        <el-form-item label="收藏量">
          <el-input v-model="form.collectionVolume" placeholder="请输入收藏量" />
        </el-form-item>
        <el-form-item label="发布地点" prop="addr">
          <el-input v-model="form.addr" placeholder="请输入发布地点" />
        </el-form-item>
        <el-form-item label="发布板块">
          <el-radio-group v-model="form.relatedColumn">
            <el-radio :label="1">美食</el-radio>
            <el-radio :label="2">旅游</el-radio>
            <el-radio :label="3">社群</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="板块类型"
          prop="relateType"
          v-if="form.relatedColumn == 1"
        >
          <el-select
            v-model="form.relateType"
            placeholder="请选择发布板块类型"
            style="width: 100%"
          >
            <el-option
              v-for="item in storeType"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="板块类型"
          prop="relateType"
          v-if="form.relatedColumn == 2"
        >
          <el-select
            v-model="form.relateType"
            placeholder="请选择发布板块类型"
            style="width: 100%"
          >
            <el-option
              v-for="item in tourType"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="板块类型"
          prop="relateType"
          v-if="form.relatedColumn == 3"
        >
          <el-select
            v-model="form.relateType"
            placeholder="请选择发布板块类型"
            style="width: 100%"
          >
            <el-option
              v-for="item in shequnType"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="团购商品" prop="goodsId">
          <el-select
            v-model="form.goodsId"
            placeholder="请选择要绑定的团购商品"
            style="width: 100%"
          >
            <el-option
              v-for="item in goodInfoList"
              :key="item.id"
              :label="item.brand + ' - ' + item.goodName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="作品标签" prop="vlogLabel">
          <el-input
            v-model="form.vlogLabel"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="审核结果" v-if="isShow == 0 && showuploads == 1">
          <el-radio-group v-model="form.isAudit">
            <el-radio label="1">通过</el-radio>
            <el-radio label="2">未通过</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listVlog,
  getVlog,
  delVlog,
  addVlog,
  updateVlog,
} from "@/api/system/vlog";
import { listGoodInfo } from "@/api/system/goodInfo";
import { listUser } from "@/api/system/userVo";
import { listType } from "@/api/system/storeType";
import { listType2 } from "@/api/system/tourType";
import { sqListType } from "@/api/system/sqtype";

export default {
  name: "Vlog",
  data() {
    return {
      dialogVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户发布信息表格数据
      vlogList: [],
      vlogList2: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        title: null,
        resType: null,
        addr: null,
        relatedColumn: null,
        relateType: null,
        subId: null,
        vlogLabel: null,
        vlogStatus: null,
        isAudit: null,
      },
      // 表单参数
      form: {},
      goodInfoList: [],
      userList: [], //发布者
      storeType: [], //美食类型
      tourType: [], //旅游类型
      shequnType: [], //社群类型
      showuploads: 0,
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "作者ID不能为空", trigger: "blur" },
        ],
        title: [{ required: true, message: "标题不能为空", trigger: "blur" }],
        resType: [
          {
            required: true,
            message: "资料类型 1图片 2视频不能为空",
            trigger: "change",
          },
        ],
        coverIcon: [
          { required: true, message: "封面图不能为空", trigger: "blur" },
        ],
        relatedColumn: [
          {
            required: true,
            message: "关联栏目 1美食  2旅游 3社群不能为空",
            trigger: "blur",
          },
        ],
        relateType: [
          {
            required: true,
            message: "二级栏目类型不能为空",
            trigger: "change",
          },
        ],
        vlogStatus: [
          {
            required: true,
            message: "作品状态  1发布 2草稿不能为空",
            trigger: "change",
          },
        ],
        isAudit: [
          {
            required: true,
            message: "审核状态 0未审核 1通过 2不通过不能为空",
            trigger: "change",
          },
        ],
      },
      isShow: 0,
      currentId: 0,
    };
  },
  created() {
    this.getList();
    this.getList2();
    this.getList3();
    this.getList4();
    this.getList5();
    this.getList6();
    this.getSqTypeList();
  },
  methods: {
    // changeIsAudit(item) {
    //   this.dialogVisible = true;
    //   this.currentId = item.id;
    // },
    // //修改审核状态为不通过
    // handleChangeAduit() {
    //   console.log(123123, this.currentId);
    //   updateVlog({id:this.currentId,isAudit:2}).then((response) => {
    //     this.$modal.msgSuccess("修改成功");
    //     this.dialogVisible = false
    //     this.getList();
    //   });
    // },
    changeisDis(row) {
      updateVlog(row).then((res) => {
        this.$modal.msgSuccess("修改成功");
        this.getList();
      });
    },
    /** 查询用户发布信息列表 */
    getList() {
      this.loading = true;
      listVlog(this.queryParams).then((response) => {
        this.vlogList = response.rows;
        // this.vlogList2 = this.distinct(this.vlogList,'userId')
        this.total = response.total;
        this.loading = false;
      });
    },
    getList2() {
      this.loading = true;
      listVlog().then((response) => {
        this.vlogList2 = this.distinct(this.vlogList, "userId");
        console.log(this.vlogList2, 'this.vlogList2')
        this.loading = false;
      });
    },
    /** 查询团购商品管理列表 */
    getList3() {
      this.loading = true;
      listGoodInfo({
        pageNum: 1,
        pageSize: 999,
      }).then((response) => {
        // response.rows.forEach((item) => {
        //   this.goodInfoList.push({
        //     value: item.id,
        //     label: item.brand,
        //   });
        // });
        this.goodInfoList = response.rows;
        this.loading = false;
      });
    },
    /** 查询客户信息列表 */
    getList4() {
      this.loading = true;
      listUser({
        pageNum: 1,
        pageSize: 999,
      }).then((response) => {
        this.userList = response.rows;
        this.loading = false;
      });
    },
    /** 查询美食门店类型信息列表 */
    getList5() {
      this.loading = true;
      listType(this.queryParams).then((response) => {
        this.storeType = response.rows;
        this.loading = false;
      });
    },
    /** 查询旅游类型信息列表 */
    getList6() {
      this.loading = true;
      listType2(this.queryParams).then((response) => {
        this.tourType = response.rows;
        this.loading = false;
      });
    },
    // 社群类型
    getSqTypeList() {
      this.loading = true;
      sqListType().then((response) => {
        this.shequnType = response.rows;
        this.loading = false;
      });
    },
    //去重
    distinct(arr, key) {
      let newobj = {};
      arr = arr.reduce((preVal, curVal) => {
        newobj[curVal[key]] ? "" : (newobj[curVal[key]] = preVal.push(curVal));
        return preVal;
      }, []);
      return arr;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        title: null,
        sceneDesc: null,
        resType: null,
        coverIcon: null,
        resContent: null,
        addr: null,
        relatedColumn: null,
        relateType: null,
        subId: null,
        vlogLabel: null,
        vlogStatus: null,
        isAudit: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户发布信息";
      this.showuploads = 0;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getVlog(id).then((response) => {
        this.form = response.data;
        this.form.isAudit = response.data.isAudit.toString();
        this.isShow = response.data.isAudit;
        this.open = true;
        this.showuploads = 1;
        this.title = "用户发布信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateVlog(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVlog({
              ...this.form,
              isAudit: 1,
              resType: 1,
              vlogStatus: 1,
            }).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除用户发布信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delVlog(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/vlog/export",
        {
          ...this.queryParams,
        },
        `vlog_${new Date().getTime()}.xlsx`
      );
    },
    // 导入数据
    handleDaoru() {
      this.open2 = true;
    },
  },
};
</script>
